unit EmployeeSelForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, ImgList, StdCtrls, Grids, DBGrids, ExtCtrls, ComCtrls,
  DataType, DepartmentHandle;

type


  TSelectedEmployee = record
    EmployeeID: integer;
    Employee: string;

  end;


  TOnSelectEmployee = procedure(aSelEmployee: TSelectedEmployee) of object;




  TfrmEmployeeSel = class(TForm)
    spl1: TSplitter;
    tvDepartment: TTreeView;
    pnlMain: TPanel;
    dbgrdEmployee: TDBGrid;
    pnl2: TPanel;
    Label1: TLabel;
    btnCancel: TButton;
    ilType: TImageList;
    qryEmployee: TADOQuery;
    dsEmployee: TDataSource;
    btnOk: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure dbgrdEmployeeDblClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure tvDepartmentChange(Sender: TObject; Node: TTreeNode);
  private
    { Private declarations }
    FSQL: TSQLRecord;
    FDepartmentHandle: TDepartmentHandle;
    //FSelectedEmployee: TSelectedEmployee;

    procedure LoadDepartment;
    procedure UpdateDBData;
    procedure QryData;
    procedure SelectData;
    procedure SetSelectedEmployee(const Value: TSelectedEmployee);

  public
    { Public declarations }
    SelectedEmployee: TSelectedEmployee;// read FSelectedEmployee write SetSelectedEmployee;
  end;


var
  frmEmployeeSel: TfrmEmployeeSel;


implementation

uses AppADO, TableData, TreeDataHandle;

{$R *.dfm}

{ TfrmEmployeeSel }

procedure TfrmEmployeeSel.btnOkClick(Sender: TObject);
begin
  SelectData;
end;

procedure TfrmEmployeeSel.dbgrdEmployeeDblClick(Sender: TObject);
begin
  SelectData;
end;

procedure TfrmEmployeeSel.FormCreate(Sender: TObject);
begin
  FDepartmentHandle := TDepartmentHandle.Create(GetADOConn());

  qryEmployee.Connection := GetADOConn();

  FSQL.SQL := 'Select * from ' + C_ViewUsed[View_Employee] + ' ';
  FSQL.Where := '';
  FSQL.Order := 'EmployeeNumber';
  FSQL.IsASC := True;

end;

procedure TfrmEmployeeSel.FormShow(Sender: TObject);
begin
  LoadDepartment;
  UpdateDBData;


end;

procedure TfrmEmployeeSel.LoadDepartment;
begin
  FDepartmentHandle.LoadTreeData(tvDepartment.Items);
end;

procedure TfrmEmployeeSel.QryData;
begin
  qryEmployee.Close;
  qryEmployee.SQL.Text := FSQL.SQL + ' ' + FSQL.Where + ' order by ' +
    FSQL.Order + ' ' + C_DBOrder[FSQL.IsASC];
  qryEmployee.Open;
end;

procedure TfrmEmployeeSel.SelectData;
var
  aSel: TSelectedEmployee;
begin

  with SelectedEmployee do
  begin

    if qryEmployee.RecordCount = 0 then
    begin
      EmployeeID := 0;
      Employee := '';
    end
    else
    begin
      EmployeeID := qryEmployee.FieldByName('EmployeeID').AsInteger;
      Employee := qryEmployee.FieldByName('EmployeeName').AsString;
    end;
  end;

  ModalResult := mrOk;

end;


procedure TfrmEmployeeSel.SetSelectedEmployee(const Value: TSelectedEmployee);
begin
  SelectedEmployee := Value;
end;

procedure TfrmEmployeeSel.tvDepartmentChange(Sender: TObject; Node: TTreeNode);
begin
  UpdateDBData;
end;

procedure TfrmEmployeeSel.UpdateDBData;
var
  aTreeData: TTreeNodeData;
begin
  if (not Assigned(tvDepartment.Selected)) or (tvDepartment.Selected.Level = 0) then
  begin
    FSQL.Where := '';
  end
  else
  begin
    aTreeData := TTreeNodeData(tvDepartment.Selected.Data);
    FSQL.Where := ' where DepartmentID >= ' + aTreeData.KeyValue +
      ' and DepartmentID < ' + IntToStr(GetNextTreeNodeID(StrToInt(aTreeData.KeyValue)));
  end;

  QryData;

end;

end.
